// stdx/opsx.rs
// CRITICAL: 运算符重载相关 trait（Add, Sub, Deref 等）示例。
// AUTO_COMMENT: 展示如何为自定义类型实现运算符。

#[allow(dead_code)]
pub fn stdx_examples_opsx() {
    // CRITICAL: 基础运算符重载示例
    use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
    #[derive(Debug)]
    struct A(i32);
    impl Add for A {
        type Output = A;
        fn add(self, rhs: Self) -> Self::Output {
            A(self.0 + rhs.0)
        }
    }
    impl Sub for A {
        type Output = A;
        fn sub(self, rhs: Self) -> Self::Output {
            A(self.0 - rhs.0)
        }
    }
    impl Mul for A {
        type Output = A;
        fn mul(self, rhs: Self) -> Self::Output {
            A(self.0 * rhs.0)
        }
    }
    impl Div for A {
        type Output = A;
        fn div(self, rhs: Self) -> Self::Output {
            A(self.0 / rhs.0)
        }
    }
    impl Rem for A {
        type Output = A;
        fn rem(self, rhs: Self) -> Self::Output {
            A(self.0 % rhs.0)
        }
    }
    impl Neg for A {
        type Output = A;
        fn neg(self) -> Self::Output {
            A(-self.0)
        }
    }

    // 使用示例
    let a1 = A(10);
    let a2 = A(3);
    let add_result = a1 + a2;
    let sub_result = A(10) - A(3);
    let mul_result = A(10) * A(3);
    let div_result = A(10) / A(3);
    let rem_result = A(10) % A(3);
    let neg_result = -A(10);

    // CRITICAL: 位运算符重载示例
    use std::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
    impl BitAnd for A {
        type Output = A;
        fn bitand(self, rhs: Self) -> Self::Output {
            A(self.0 & rhs.0)
        }
    }
    impl BitOr for A {
        type Output = A;
        fn bitor(self, rhs: Self) -> Self::Output {
            A(self.0 | rhs.0)
        }
    }
    impl BitXor for A {
        type Output = A;
        fn bitxor(self, rhs: Self) -> Self::Output {
            A(self.0 ^ rhs.0)
        }
    }
    impl Shl<i32> for A {
        type Output = A;
        fn shl(self, rhs: i32) -> Self::Output {
            A(self.0 << rhs)
        }
    }
    impl Shr<i32> for A {
        type Output = A;
        fn shr(self, rhs: i32) -> Self::Output {
            A(self.0 >> rhs)
        }
    }
    impl Not for A {
        type Output = A;
        fn not(self) -> Self::Output {
            A(!self.0)
        }
    }

    // 使用示例
    let bitand_result = A(10) & A(3);
    let bitor_result = A(10) | A(3);
    let bitxor_result = A(10) ^ A(3);
    let shl_result = A(10) << 2;
    let shr_result = A(10) >> 2;
    let not_result = !A(10);

    // CRITICAL: 比较运算符重载示例
    use std::cmp::Ordering;
    impl PartialEq for A {
        fn eq(&self, other: &Self) -> bool {
            self.0 == other.0
        }
    }
    impl PartialOrd for A {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            self.0.partial_cmp(&other.0)
        }
    }

    // 使用示例
    let eq_result = A(10) == A(10);
    let cmp_result = A(10) < A(5);

    // CRITICAL: 索引运算符重载示例
    use std::ops::{Index, IndexMut};
    #[derive(Debug)]
    struct MyVec(Vec<i32>);
    impl Index<usize> for MyVec {
        type Output = i32;
        fn index(&self, index: usize) -> &Self::Output {
            &self.0[index]
        }
    }
    impl IndexMut<usize> for MyVec {
        fn index_mut(&mut self, index: usize) -> &mut Self::Output {
            &mut self.0[index]
        }
    }

    // 使用示例
    let mut my_vec = MyVec(vec![1, 2, 3]);
    let indexed = my_vec[1];
    my_vec[1] = 20;

    // CRITICAL: Deref 和 DerefMut 示例
    use std::ops::{Deref, DerefMut};
    impl Deref for MyVec {
        type Target = Vec<i32>;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }
    impl DerefMut for MyVec {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.0
        }
    }

    // 使用示例
    let deref_len = my_vec.len();

    // 结果不会被使用，避免编译器优化掉
    let _ = (
        add_result,
        sub_result,
        mul_result,
        div_result,
        rem_result,
        neg_result,
        bitand_result,
        bitor_result,
        bitxor_result,
        shl_result,
        shr_result,
        not_result,
        eq_result,
        cmp_result,
        indexed,
        deref_len,
    );
}
